package com.travel.common.base;


import com.travel.common.exception.LogicException;
import lombok.Data;
import org.omg.CORBA.SystemException;

import java.io.Serializable;

/**
 * rest api 返回对象
 *
 * @author fengchu
 * @date 2018-12-18 15:04
 */
@Data
public class Resp<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String OK_MSG = "ok";
    private static final int OK_CODE = 200;
    private static final int ERROR_CODE = 500;

    private int code;
    private String msg;
    private T data;

    public Resp() {
    }

    public  Resp(Integer code,String msg,T data) {
        super();
        this.code = OK_CODE;
        this.msg = OK_MSG;
        this.data = data;
    }
    private Resp(T data) {
        super();
        this.code = OK_CODE;
        this.msg = OK_MSG;
        this.data = data;
    }

    private Resp(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = ERROR_CODE;
        this.data = null;
    }

    private Resp(Throwable e, int code) {
        super();
        this.msg = e.getMessage();
        this.code = code;
        this.data = null;
    }

    private Resp(String msg, int code) {
        super();
        this.msg = msg;
        this.code = code;
        this.data = null;
    }

    public static <T> Resp<T> ok(T data) {

        return new Resp<T>(data);
    }

    public static <T> Resp<T> ok() {

        return new Resp<T>((T) null);
    }

    public static <T> Resp<T> ok(String msg, int code) {

        return new Resp<T>(msg, code);
    }

    public static <T> Resp<T> error(Throwable e) {

        if (e instanceof LogicException || e instanceof SystemException ) {
            LogicException logicException = (LogicException) e;
            return new Resp<T>(logicException.getMsg(), logicException.getCode());
        }

        return new Resp<T>(e);
    }

    public static <T> Resp<T> error(Throwable e, int code) {

        return new Resp<T>(e, code);
    }

    public static <T> Resp<T> error(String msg, int code) {

        return new Resp<T>(msg, code);
    }

 /*   public static <T> Resp<T> error(ExceptionEnum exceptionEnum) {

        return new Resp<T>(exceptionEnum.getMsg(), exceptionEnum.getCode());
    }*/
}